Interface (object-oriented programming)
part 4/6 · 9.9 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
An example of syntax for interfaces may look like the following:
class Animal { ... }
class Theropod extends Animal { ... }
interface Flyable {
void fly();
}
interface Vocal {
void vocalize();
}
public class Bird extends Theropod implements Flyable, Vocal {
// ...
public void fly() { ... }
public void vocalize() { ... }
}
In languages without explicit support, interfaces are often still present as conventions; this is known as duck typing. For example, in Python, any class can implement an __iter__ method and be used as a collection.cite-ref-python-iter-4-0[3]
Type classes in languages like Haskell, or module signatures in ML and OCaml, are used for many of the things that interfaces are used for.
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────